/*
 * material_image.cpp
 *
 *  Created on: 15.06.2012
 *      Author: Caffeine
 */


#include "material_image.h"
#include <lampe/xml/IXMLFactoryEnvironment.h>
#include "irrxml.h"
#include <lampe/xml/IXmlNode.h>
#include <ISceneNodeAnimator.h>
#include <ISceneManager.h>

namespace lampe{
namespace xml{
class MaterialImageFactory:public IIrrXmlNode{
public:
	MaterialImageFactory(irr::IrrlichtDevice*d):IIrrXmlNode(d){}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> list;
		list.push_back("material:image");
		return list;
	};

	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		if (!node->getParent() || !node->getParent()->getTag())
			return lampe::xml::STOP;
		u32 index=node->getAttS32("index",0);
		String href=node->getAttString("href","");
		if (href=="")
			return lampe::xml::STOP;
		irr::video::SMaterial*mat=(irr::video::SMaterial*)node->getParent()->getTag();
		mat->setTexture(index,getDevice()->getVideoDriver()->getTexture(href.c_str()));
		//mat->setTexture(index,GameEnvironment::getDriver()->getTexture(irr::utils::getAbsolutePath(href)));
		return lampe::xml::NEXT;
	}
};

}//namespace xml
}//namespace lampe

void regMaterialImage(lampe::xml::IXMLFactoryEnvironment*environment,irr::IrrlichtDevice*d){
	lampe::xml::IFactoryRecord*h=new lampe::xml::MaterialImageFactory(d);
	environment->registerHandler(h);
	h->drop();
}
